#!/usr/bin/env python


from data_center import DataCenter
from config import TestCaseManager,zmqports
from statemachine_conn import StateMachineConn
from test_case_report import TestCaseReport
from command_list_report import CommandListReport
from threading import Thread
from time import sleep

class IntegrateTestServer(Thread):
	def __init__(self,config,publisher):
		super(IntegrateTestServer, self).__init__()

		ip_list = config["ip_list"]
		port_list = config["port_list"]
		trigger_port = config["trigger_port"]
		sm_port = config["sm_port"]
		sm_state_port = config["sm_state_port"]
		sequencer_port = config["sequencer_port"]

		self.test_case_manager_ = TestCaseManager()
		self.dc_ = 	DataCenter(ip_list,port_list)
		self.sm_ = StateMachineConn(trigger_port,sm_port,sm_state_port)
		self.test_case_report_ =  TestCaseReport(sequencer_port,publisher=publisher)
		self.command_list_report_ = CommandListReport()
		self.publisher_ = publisher
		self.setDaemon(True) 

	def waitConfirmSignal(self,flag_str='Y'):
		while True:
			print("Input 'Y' to start Integrated System test...")
			input_str = raw_input()
			if (input_str.upper()==flag_str.upper()):
				return True

	def loadDefaultMapTable(self):
		default_mt_id = self.test_case_manager_.getDefaultMapTableID()
		arm_map_table = self.test_case_manager_.getTestCaseForArm(default_mt_id)
		power_map_table = self.test_case_manager_.getTestCaseForPower(default_mt_id)
		dut_map_table = self.test_case_manager_.getTestCaseForDut(default_mt_id)
		base_id_regex = self.test_case_manager_.getBaseIDRegex(default_mt_id)
		self.dc_.setMapTable(arm_map_table,power_map_table,dut_map_table,base_id_regex)


	def run(self):
		print("Begin travel test-case.........")
		msg_header = "Group,Tid,TestCaseName,Hit,Pass,Expected_result,Real_result"
		self.publisher_.publish(zmqports.TESTSERVER_PUB_CHANNEL, msg_header)
		num_max = self.test_case_manager_.getTestCaseLength()

		self.loadDefaultMapTable()
		self.dc_.launchServer()
		# wait user input 'Y' to begin
		self.waitConfirmSignal(flag_str="Y")
		#
		for i in range(num_max):
			testcase_name = self.test_case_manager_.getTestCaseName(i)
			print("test-case:  >>>>>>>>>>> {0}".format(testcase_name))
			# Load test case to DataCenter
			arm_map_table = self.test_case_manager_.getTestCaseForArm(i)
			power_map_table = self.test_case_manager_.getTestCaseForPower(i)
			dut_map_table = self.test_case_manager_.getTestCaseForDut(i)
			expected = self.test_case_manager_.getExpected(i)
			base_id_regex = self.test_case_manager_.getBaseIDRegex(i)
			self.dc_.setMapTable(arm_map_table,power_map_table,dut_map_table,base_id_regex)
			# start_sm
			self.sm_.startTM()
			# start_listen_test_case_result
			self.test_case_report_.startListen()
			# wait_for_sm
			self.sm_.waitForEnd()
			# build report
			self.test_case_report_.endListen()
			self.test_case_report_.createReport(expected,testcase_name=testcase_name)

		print("End travel test-case. Now it's echo server.")
		self.loadDefaultMapTable()
